/**
  ******************************************************************************
  * @file    Libraries/Device/TS32Fx/ts32fx_system.h
  * @author  TOPSYS Application Team
  * @version V1.0.0
  * @date    02-11-2018
  * @brief   CMSIS Cortex-M0 Device Peripheral Access Layer Header File.
  *          This file contains all the peripheral register's definitions, bits
  *          definitions and memory mapping for TS32Fx Connectivity line.
  *          The file is the unique include file that the application programmer
  *          is using in the C source code, usually in main.c. This file contains:
  *           - Data structures and the address mapping for all peripherals
  *           - Peripheral's registers declarations and bits definition
  *           - Macros to access peripherals registers hardware
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 TOPSYS</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TS32FX_SYSTEM_H
#define __TS32FX_SYSTEM_H

#ifdef __cplusplus
 extern "C" {
#endif

/** @addtogroup CMSIS
  * @{
  */

/** @defgroup   CONFIG_SETTING CONFIG_SETTING
  * @brief Various configuration settings
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "typedef.h"
#include "ts32fx.h"

/* Private define ------------------------------------------------------------*/
/*! SDK Version V2.06    2020-05-15
 */
#define __SDK_VERSION                       0x010001UL
/*! FPGA use
 */
#define FPGA_EN                             0
#define CRC_CHECK_EN                        1
#define ALL_WORD_FF                         0xFFFFFFFF
#define ALL_BYTE_FF                         0xFF
#define ALL_WORD_00                         0x00000000

/*! adc useing non mass production  
 *  You must must close this macro ,if it is formal Mass production chip(chip already do it itself),  
 *  or else it is very very serious mistake.  
 */
#define USE_ADC_NON_MASS_PRODUCT            0
/*! using 48 pin chip gpio remap status  
 */
  
#define USE_CHIP_16_PIN                     0
#define USE_CHIP_20_PIN                     0
#define USE_CHIP_24_PIN                     0
#define USE_CHIP_28_PIN                     1
#define USE_CHIP_48_PIN                     0

#if (USE_CHIP_48_PIN || USE_CHIP_16_PIN)
/*! no remap adc channel for pb pin
 */
#define __CHIP_PB_NO_REMAP
#endif

#if FPGA_EN
    #define SYS_CLK                         48000000UL
#else
    #define SYS_CLK                         52000000UL
#endif
     
//#pragma import(__use_no_semihosting) 

#define DEBUG_LEVEL                         DL_DGB
#define DMA_DBG_EN                          0
#define SD_DBG_EN                           1
#define LCD_DBG_EN                          0
#define SPI_DBG_EN                          1
#define IIC_DBG_EN                          1

/*********************************************************************************/
// system debug level : normal \ warning \ error \ none
/*********************************************************************************/
#define DL_NONE                             0
#define DL_ERR                              1
#define DL_WARNING                          2
#define DL_DGB                              3

#if (DL_NONE == DEBUG_LEVEL)
    #define DBG_ERR_EN                      0
    #define DBG_WAR_EN                      0
    #define DBG_NOR_EN                      0
#elif (DL_ERR == DEBUG_LEVEL)
    #define DBG_ERR_EN                      1
    #define DBG_WAR_EN                      0
    #define DBG_NOR_EN                      0
#elif (DL_WARNING == DEBUG_LEVEL)
    #define DBG_ERR_EN                      1
    #define DBG_WAR_EN                      1
    #define DBG_NOR_EN                      0
#elif (DL_DGB == DEBUG_LEVEL)
    #define DBG_ERR_EN                      1
    #define DBG_WAR_EN                      1
    #define DBG_NOR_EN                      1
#endif

/* Private macro -------------------------------------------------------------*/
#if (DBG_ERR_EN)
    #define DBG_ERR(fmt, args...)           printf("[err]");       printf(fmt, ##args)
    #define DBG_ERR_BUF(buf, len)           printf_array(buf, len)
#else
    #define DBG_ERR(...)
    #define DBG_ERR_BUF(buf, len)
#endif

#if (DBG_WAR_EN)
    #define DBG_WAR(fmt, args...)           printf("[warning]");   printf(fmt, ##args)
    #define DBG_WAR_BUF(buf, len)           printf_array(buf, len)
#else
    #define DBG_WAR(...)
    #define DBG_WAR_BUF(buf, len)
#endif

#if (DBG_NOR_EN)
    #define DBG_NOR(fmt, args...)           printf(fmt, ##args)
    #define DBG_NOR_BUF(buf, len)           printf_array(buf, len)
#else
    #define DBG_NOR(...)
    #define DBG_NOR_BUF(buf, len)
#endif

/*********************************************************************************/
// define each peripheral debug info
/*********************************************************************************/
#if (DMA_DBG_EN)
    #define DMA_DBG_NOR(fmt, args...)       DBG_NOR(fmt, ##args)
    #define DMA_DBG_NOR_BUF(buf, len)       DBG_NOR_BUF(buf, len)
#else
    #define DMA_DBG_NOR(...)
    #define DMA_DBG_NOR_BUF(buf, len)
#endif

#if (SD_DBG_EN)
    #define SD_DBG_NOR(fmt, args...)        DBG_NOR(fmt, ##args)
    #define SD_DBG_NOR_BUF(buf, len)        DBG_NOR_BUF(buf, len)
#else
    #define SD_DBG_NOR(...)
    #define SD_DBG_NOR_BUF(buf, len)
#endif

#if (LCD_DBG_EN)
    #define LCD_DBG_NOR(fmt, args...)       DBG_NOR(fmt, ##args)
    #define LCD_DBG_NOR_BUF(buf, len)       DBG_NOR_BUF(buf, len)
#else
    #define LCD_DBG_NOR(...)
    #define LCD_DBG_NOR_BUF(buf, len)
#endif

#if (SPI_DBG_EN)
    #define SPI_DBG_NOR(fmt, args...)       DBG_NOR(fmt, ##args)
    #define SPI_DBG_NOR_BUF(buf, len)       DBG_NOR_BUF(buf, len)
#else
    #define SPI_DBG_NOR(...)
    #define SPI_DBG_NOR_BUF(buf, len)
#endif

#if (IIC_DBG_EN)
    #define IIC_DBG_NOR(fmt, args...)       DBG_NOR(fmt, ##args)
    #define IIC_DBG_NOR_BUF(buf, len)       DBG_NOR_BUF(buf, len)
#else
    #define IIC_DBG_NOR(...)
    #define IIC_DBG_NOR_BUF(buf, len)
#endif

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/* Private variables ---------------------------------------------------------*/
/* extern u32 __vector_handlers; */
extern volatile u32 SysTickCnt;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Setup the microcontroller system
  *         Initialize the Embedded Flash Interface, the PLL and update the 
  *         SystemCoreClock variable.
  * @note   This function should be used only after reset.
  * @param  None
  * @retval None
  */
void SystemInit(void);
/**
  * @brief  is_systick_expired.
  * @param  offset_ticks: current sys tick 
  * @param  Texpire: offset ticks
  * @retval return true or fault
  */
u32 is_systick_expired(s32 offset_ticks, s32 Texpire);
/**
  * @brief  SystemTickInit.
  * @param  None
  * @retval return true or fault
  */
u32 SystemTickInit(void);
/**
  * @brief  GetSysTick.
  * @param  None
  * @retval return the count of current systick
  */
u32 GetSysTick(void);

/**
  * @brief  Use the CPU to delay approximately ms
  * @param  n : the number of ms to delay
  * @retval None
  */
void delay_ms(u32 n);

/**
  * @brief  Use the CPU to delay approximately us
  * @param  n : the number of us to delay
  * @retval None
  */
void delay_us(u32 n);

/**
  * @brief sys_special function init
  * @param None
  * @retval None 
  */
void sys_special_fun_init(void);

#ifdef __cplusplus
}
#endif

#endif //__TS32FX_SYSTEM_H

/*************************** (C) COPYRIGHT 2018 TOPSYS ***** END OF FILE *****/
